package fag.editor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FrameDialog extends JDialog {

	private static final long serialVersionUID = 882948425226838596L;
	private String nom = "";
	private Integer ms = 0;
	private boolean saisieOK = false;
	private JButton btOk = new JButton("OK");
	private JButton btCancel = new JButton("Cancel");
	private JTextField tfNom = new JTextField("");
	private JFormattedTextField ftfMs = new JFormattedTextField(NumberFormat.getIntegerInstance());
	private JPanel panelField = new JPanel();
	private JPanel panelControl = new JPanel();
	

	public FrameDialog(JFrame f) {
		
		super(f,"Popup Editor",true);
		this.setSize(250,220);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	    
		JPanel panNom = new JPanel();
		panNom.setBackground(Color.white);
		panNom.setPreferredSize(new Dimension(220, 60));
		tfNom.setPreferredSize(new Dimension(100, 25));
		panNom.setBorder(BorderFactory.createTitledBorder("Nom de la Frame"));
		JLabel nomLabel = new JLabel("Saisir un nom :");
		panNom.add(nomLabel);
		panNom.add(tfNom);
		
		JPanel panMs = new JPanel();
		panMs.setBackground(Color.white);
		panMs.setPreferredSize(new Dimension(220, 60));
		ftfMs.setPreferredSize(new Dimension(100, 25));
		panMs.setBorder(BorderFactory.createTitledBorder("Durée de la Frame"));
		JLabel msLabel = new JLabel("Saisir la durée :");
		panMs.add(msLabel);
		panMs.add(ftfMs);
		
		panelField.add(panNom);
		panelField.add(panMs);
		panelControl.add(btOk);
		panelControl.add(btCancel);
		this.getContentPane().add(panelField,BorderLayout.CENTER);
		this.getContentPane().add(panelControl,BorderLayout.SOUTH);
		
		btOk.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				
				if((!(tfNom.getText().equals(""))) && (!(ftfMs.getText().equals("")))) {
					
					saisieOK = true;
					nom = tfNom.getText();
					ms = Integer.parseInt(ftfMs.getText());
					setVisible(false);
					JOptionPane.showMessageDialog(null,
							  "Frame crée",
							  "Popup Editor",
							  JOptionPane.INFORMATION_MESSAGE);
				}
				
				else {
					
					saisieOK = false;
					JOptionPane.showMessageDialog(null,
							  "Saisie incorrecte",
							  "Popup Editor",
							  JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		btCancel.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
			}
		});
		
		
	}

	public void showFrameDialog() {
		
		saisieOK = false;
		tfNom.setText("");
		ftfMs.setText("");
		nom = "";
		ms = 0;
		setVisible(true);
		repaint();
	}

	public Integer getMs() {
		
		return ms;
	}

	public void setMs(Integer i) {
		
		ms = i;
	}

	public String getNom() {
		
		return nom;
	}

	public void setNom(String s) {
		
		nom = s;
	}

	public boolean isSaisieOK() {
		
		return saisieOK;
	}
}